package YAPserver.logic;

import java.util.HashMap;

/**
 * This class handles input that comes indirectly from a client into the game during runtime.
 * @author Daniel
 *
 */
public class Input{
	
	private HashMap<Integer, Player> m_players;
	private HashMap<Integer, MoveCommand> m_commands;
	
	/**
	 * Constructs the input
	 * @param players a hashMap of players
	 */
	public Input(HashMap<Integer, Player> players){
		m_commands = new HashMap<Integer, MoveCommand>();
		for(Integer id : players.keySet()){
			m_commands.put(id ,MoveCommand.NOMOVE);
		}
	}
	
	/**
	 * There are 3 different move commands: LEFT, RIGHT and NOMOVE.
	 * These are used to tell the slider to move left, right or not at all.
	 * @author Daniel
	 *
	 */
	public enum MoveCommand {
		LEFT, RIGHT, NOMOVE;
	}
	
	/**
	 * Puts the commands in a hashMap - the id of the player is the key and the command (LEFT, RIGHT or NOMOVE) is the Value.
	 * 
	 * @param id id of the player whose command is to be set
	 * @param command moveCommand to be set
	 */
	synchronized public void setCommand(int id, MoveCommand command){
		m_commands.put(id, command);
	}
	
	/**
	 * Sets the hashMap of players.
	 * @param players a hashMap of players
	 */
	public void setPlayers(HashMap<Integer, Player> players){
		m_players = players;
	}
	
	/**
	 * Sets the move command of each player using this input's hashMap of moveCommands.
	 * From the player class the slider can then be moved.
	 */
	synchronized public void sendCommands(){
//		synchronized(this){
			for(int id : m_commands.keySet()){
				if(m_players.containsKey(id)){
					m_players.get(id).setMoveCommand(m_commands.get(id));
				}
			}
//		}
	}
	
	/**
	 * Returns the hashMap of moveCommands.
	 * @return the hashMap of moveCommands
	 */
	public HashMap<Integer, MoveCommand> getCommands(){
		return m_commands;
	}
	
	
	


}
